home *** CD-ROM | disk | FTP | other *** search
/ Technotools / Technotools (Chestnut CD-ROM)(1993).ISO / lang_oth / factoral / factoral.ada
Text File  |  1988-08-31  |  2KB  |  49 lines

  1. with text_io;
  2. --***********************************************
  3. -- Recursive Factorial                          *
  4. -- Guy Cox                                      *
  5. -- Version 1.0                                  *
  6. -- 17 May 1988                                  *
  7. -- Inputs:                                      *
  8. --      n - natural number                      *
  9. -- Outputs                                      *
  10. --      factorial of the input number           *
  11. -- Raises:                                      *
  12. --      numeric_error on result too large       *
  13. --      data_error on incorrect data type       *
  14. --***********************************************
  15.  
  16. procedure factorial is
  17.  
  18. package fac_io is new text_io.integer_io(natural);
  19.  
  20. any_number : natural;
  21.  
  22. function fac( n : in natural) return natural is
  23. begin
  24.   if n = 0 then return 1;     -- by definition
  25.   elsif n = 1 then return 1;  -- by definition
  26.   else
  27.     return n *  fac(n - 1 );
  28.   end if;
  29.  end fac;
  30.  
  31. begin
  32.    text_io.put("Enter a number ");
  33.    fac_io.get(any_number);
  34.    fac_io.put(fac(any_number));
  35.  
  36. exception
  37.     when numeric_error =>
  38.        text_io.put("That number is larger than I can handle (");
  39.        fac_io.put(natural'last);
  40.        text_io.put(")");
  41.        text_io.new_line;
  42.      when text_io.data_error =>
  43.        text_io.put("Listen hosehead I said to enter a natural number");
  44.      when others =>
  45.        text_io.put("I am totally lost");
  46.        text_io.new_line;
  47.        raise; -- re-raise the exception so I can see the error
  48. end factorial;
  49.